package tech.wetech.admin.modules.system.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.wetech.admin.modules.base.query.PageQuery;
import tech.wetech.admin.modules.base.service.impl.BaseService;
import tech.wetech.admin.modules.system.mapper.SysMachInfoMapper;
import tech.wetech.admin.modules.system.po.SysMachInfo;
import tech.wetech.admin.modules.system.service.SysMachInfoService;
import tk.mybatis.mapper.code.Style;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author lu
 * @description
 * @date 2019年08月02日 13:30
 */
@Service
public class SysMachInfoServiceImpl extends BaseService<SysMachInfo> implements SysMachInfoService {

    @Autowired
    private SysMachInfoMapper sysMachInfoMapper;

    /*
    ** @author lu
    * @date 2019/8/2 17:25
    * @param [sysMachInfo]
    * @return void
    * @description 新增入库
    */
    @Override
    public void createSysMachInfo(SysMachInfo sysMachInfo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            sysMachInfo.setDateTime(sdf.parse(sysMachInfo.getStrDateTime()));
            sysMachInfo.setLastcommtime(sdf.parse(sysMachInfo.getStrLastcommtime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        sysMachInfoMapper.insertSelective(sysMachInfo);
    }

    @Override
    public void updateSysMachInfo(SysMachInfo sysMachInfo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            sysMachInfo.setDateTime(sdf.parse(sysMachInfo.getStrDateTime()));
            sysMachInfo.setLastcommtime(sdf.parse(sysMachInfo.getStrLastcommtime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        sysMachInfoMapper.updateByPrimaryKeySelective(sysMachInfo);
    }

    @Override
    public List<SysMachInfo> querySelectList(SysMachInfo sysMachInfo, PageQuery pageQuery) {
        if (pageQuery.getOrderBy() != null) {
            pageQuery.setOrderBy(StringUtil.convertByStyle(pageQuery.getOrderBy(), Style.camelhump));
        }

        //判断机器号是否为空，不为空进行模糊查询
        if (sysMachInfo.getMachNo()!=null){
            Example example = new Example(SysMachInfo.class);
            Example.Criteria criteria = example.createCriteria();

            String StrMachNo=sysMachInfo.getMachNo()+"%";
            // 创建条件对象
            criteria.andLike("machNo",StrMachNo);
            return PageHelper.startPage(pageQuery)
                    .doSelectPage(() -> mapper.selectByExample(example));
        }
        return PageHelper.startPage(pageQuery)
                .doSelectPage(() -> mapper.select(sysMachInfo));
    }
}
